package repository;

import java.io.File;
import java.io.IOException;
import fileoperation.FileCreation;
import fileoperation.FileDeletion;

/**
 * Todo: Add your own code. JitInit.init("worktree") should be able to create a repository "worktree/.jit" ,
 *       which contains all the default files and other repositories inside. If the repository has
 *       already exists, delete the former one and create a new one. You're welcome to reconstruct the code,
 *       but don't change the code in the core directory.
 */
public class Repository {
    private static String workTree;	//working directory
    private static String gitDir;	//jit repository path

    /**
     * Constructor
     */
    public Repository() throws IOException {
        if(gitDir == ""){
            throw new IOException("The repository does not exist!");
        }
    }
    
    /**
     * Construct a new repository instance with certain path.
     * Constructor
     * @param path
     * @throws IOException
     */
    public Repository(String path) throws IOException {
        this.workTree = path;
        this.gitDir = path + File.separator + ".jit";
    }

    public static String getGitDir() {
        return gitDir;
    }

    public static String getWorkTree() {
        return workTree;
    }
    
    /**
     * Helper functions.
     * @return
     */
    public boolean exist(){ return new File(gitDir).exists(); }

    public boolean isFile(){ return new File(gitDir).isFile(); }

    public boolean isDirectory(){ return new File(gitDir).isDirectory(); }


  /**
     * Create the repository and files and directories inside.
     * @return boolean
     * @throws IOException
     */
    public void createRepo() throws IOException {
        /* Todo：Add your code here. */

        // create the workTree/.jit
        FileCreation.createDirectory(getWorkTree(), ".jit");

        // hide workTree/.jit
        String setJitHide = "attrib +H \"" + getGitDir() + "\"";
        Runtime.getRuntime().exec(setJitHide);

        // create the subDirectory
        String [][] jitFolderName = {{"hooks", ""}, {"info", ""}, {"objects", "info"},
                {"objects", "pack"}, {"refs", "heads"}, {"refs", "tags"}};
        for(int i = 0; i < jitFolderName.length; i++)
            FileCreation.createDirectory(getGitDir(), jitFolderName[i][0], jitFolderName[i][1]);

        // create some default docs: The text content imitates the default state of GIT
        String [][]docsPathAndName = {{"info", "exclude"}, {"", "config"}, {"", "description"}, {"", "HEAD"}, {"", "COMMIT_EDITMSG"}};
        String []docsContent = {"# git ls-files --others --exclude-from=.git/info/exclude\r\n"
                + "# Lines that start with '#' are comments.\r\n"
                + "# For a project mostly in C, the following would be a good set of\r\n"
                + "# exclude patterns (uncomment them if you want to use them):\r\n"
                + "# *.[oa]\r\n"
                + "# *~",
                "[core]\r\n"
                        + " repositoryformatversion = 0\r\n"
                        + " filemode = false\r\n"
                        + " bare = false\r\n"
                        + " logallrefupdates = true\r\n"
                        + " symlinks = false\r\n"
                        + " ignorecase = true\r\n",
                "Unnamed repository; edit this file 'description' to name the repository.\r\n",
                "ref: refs/heads/master\n",""};
        for(int i = 0; i < docsPathAndName.length; i++)
            FileCreation.createFile(getGitDir() + File.separator + docsPathAndName[i][0], docsPathAndName[i][1], docsContent[i]);


    }
}
